package com.chengyu.core.controller.file;

import com.alibaba.fastjson.JSONArray;
import com.chengyu.core.component.OperationLog;
import com.chengyu.core.controller.ShopBaseController;
import com.chengyu.core.domain.form.GoodsPublishForm;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.FileInfoServiceMapper;
import com.chengyu.core.mapper.UmsShopOwnLevelMapper;
import com.chengyu.core.model.*;
import com.chengyu.core.service.file.FileInfoService;
import com.chengyu.core.service.member.MemberService;
import com.chengyu.core.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "文件管理")
@Controller
@RequestMapping("/member")
public class FileController extends ShopBaseController {
    @Autowired
    protected FileInfoService fileInfoService;
    @Autowired
    private FileInfoServiceMapper fileInfoMapper;
    @Autowired
    private UmsShopOwnLevelMapper umsShopOwnLevelMapper;

    @OperationLog
    @ApiOperation(value = "添加文件")
    @ResponseBody
    @RequestMapping(value = "/file/add", method = RequestMethod.POST)
    public CommonResult<String> add(FileInfo fileInfo, Integer sourceType) throws ServiceException {
        fileInfo.setAddTime(new Date());
        if (fileInfo.getFileType().contains("video")) {
            //如果是视频
            if (sourceType == 1) {
                //如果是素材
                fileInfo.setType(1);
            } else {
                //如果是商品
                fileInfo.setType(3);
            }
        } else if (fileInfo.getFileType().contains("image")) {
            //如果是图片
            if (sourceType == 1) {
                //如果是素材
                fileInfo.setType(2);
            } else {
                //如果是商品
                fileInfo.setType(4);
            }
        }

        fileInfo.setSource(1);
        fileInfo.setSourceId(getCurrentShopId());
        fileInfoService.add(fileInfo);
        return CommonResult.success(null);
    }

    @OperationLog
    @ApiOperation(value = "验证文件是否还能上传")
    @ResponseBody
    @RequestMapping(value = "/file/validateFileSize", method = RequestMethod.GET)
    public CommonResult<String> validateFileSize(Double uploadTotalSize) throws ServiceException {
        //容量
        List<Integer> types = new ArrayList<>();
        types.add(1);
        types.add(2);
        FileInfoExample exampleFile = new FileInfoExample();
        FileInfoExample.Criteria criteriaFile = exampleFile.createCriteria();
        criteriaFile.andSourceEqualTo(1);
        criteriaFile.andSourceIdEqualTo(getCurrentShopId());
        criteriaFile.andTypeIn(types);
        double sumSize = fileInfoMapper.sumSize(exampleFile);


        UmsShopOwnLevelExample exampleOwnLevel = new UmsShopOwnLevelExample();
        UmsShopOwnLevelExample.Criteria criteria = exampleOwnLevel.createCriteria();
        criteria.andShopIdEqualTo(getCurrentShopId());
        List<Integer> typesOwn = new ArrayList<>();
        typesOwn.add(1);
        typesOwn.add(2);
        criteria.andTypeIn(typesOwn);
        criteria.andDeadlineGreaterThan(new Date());
        List<UmsShopOwnLevel> umsShopOwnLevels = umsShopOwnLevelMapper.selectByExample(exampleOwnLevel);
        double totalNum = 0.0;
        for (UmsShopOwnLevel umsShopOwnLevel : umsShopOwnLevels) {
            totalNum = totalNum + umsShopOwnLevel.getResidueStorageQuantity() * 1073741824.0;
        }

        if (sumSize + uploadTotalSize > totalNum) {
            //如果已存在的文件大小总合大于等于会员拥有容量
            throw new ServiceException("商家会员剩余上传素材内存容量不足");
        }
        return CommonResult.success(null);
    }


}